using FytSoa.Domain.Sys;
using FytSoa.Sugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Masuit.Tools.Linq;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Application.Sys
{
    /// <summary>
    /// 字典信息表服务接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "v1")]
    public class SysCodeService : SugarRepository<SysCode>,ISysCodeService 
    {
        private readonly IMapper _mapper;
        public SysCodeService(IMapper mapper)
        {
            _mapper = mapper;
        }
        
        /// <summary>
        /// 查询所有——分页
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageResult<SysCodeDto>>> GetPagesAsync(PageParam<CommonParam> param)
        {
            Expression<Func<SysCode, bool>> where = m => true;
            if (!string.IsNullOrEmpty(param.Filter.Key))
            {
                where.And(m => m.Name.Contains(param.Filter.Key));
            }
            if (!string.IsNullOrEmpty(param.Filter.Status))
            {
                where.And(m => m.Status == bool.Parse(param.Filter.Status));
            }
            var (list,totalItems,totalPage) = await base.GetPageResultAsync(where, param.Page, param.Limit, m => m.Id);
            var result = new PageResult<SysCodeDto>()
            {
                Items = _mapper.Map<List<SysCodeDto>>(list),
                TotalItems = totalItems,
                TotalPages = totalPage
            };
            return JResult<PageResult<SysCodeDto>>.Success(result);
        }

        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<SysCodeDto>> GetAsync(long id)
        {
            return JResult<SysCodeDto>.Success(_mapper.Map<SysCodeDto>(await base.GetByIdAsync(id)));
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> AddAsync(SysCodeDto model)
        {
            return JResult<bool>.Success(await base.InsertAsync(_mapper.Map<SysCode>(model)));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> ModifyAsync(SysCodeDto model)
        {
            return JResult<bool>.Success(await base.UpdateAsync(_mapper.Map<SysCode>(model)));
        }

        /// <summary>
        /// 删除,支持多个
        /// </summary>
        /// <param name="ids">逗号分隔</param>
        /// <returns></returns>
        public async Task<ApiResult<bool>> DeleteAsync(string ids)
        {
            var id = ids.StrToListLong();
            return JResult<bool>.Success(await base.DeleteAsync(m=>id.Contains(m.Id)));
        }
    }
}
